package com.jjw.route.service.impl;

import com.jjw.route.common.LoginEnum;
import com.jjw.route.dao.UserDao;
import com.jjw.route.entity.UserEntity;
import com.jjw.route.to.BarrageTo;
import com.jjw.route.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jjw.route.utils.PageUtils;
import com.jjw.route.utils.Query;

import com.jjw.route.dao.BarrageDao;
import com.jjw.route.entity.BarrageEntity;
import com.jjw.route.service.BarrageService;


@Service("barrageService")
public class BarrageServiceImpl extends ServiceImpl<BarrageDao, BarrageEntity> implements BarrageService {
    @Autowired
    BarrageDao barrageDao;
    @Autowired
    UserDao userDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BarrageEntity> page = this.page(
                new Query<BarrageEntity>().getPage(params),
                new QueryWrapper<BarrageEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R insert(BarrageTo barrageTo) {
        BarrageEntity barrageEntity = new BarrageEntity();
        barrageEntity.setContent(barrageTo.getContent());
        int insert = barrageDao.insert(barrageEntity);
        if (insert != 0) return R.ok();
        return R.error(LoginEnum.UNKNOW_EXCEPTION.getCode(), LoginEnum.UNKNOW_EXCEPTION.getMessage());
    }

    @Override
    public R getData() {
        List<UserEntity> userEntities = userDao.selectList(new QueryWrapper<UserEntity>());
        List<BarrageEntity> barrageEntities = new ArrayList<>();
        for (UserEntity userEntity : userEntities) {
            BarrageEntity e = new BarrageEntity();
            e.setContent(userEntity.getName() + "到此一游！");
            barrageEntities.add(e);
            barrageDao.insert(e);
        }
        return R.ok();
    }

    @Override
    public R getList() {
        List<BarrageEntity> barrageEntities = barrageDao.selectList(new QueryWrapper<BarrageEntity>());
        List<String> arr = new ArrayList<>();
        for (BarrageEntity barrageEntity : barrageEntities) {
            arr.add(barrageEntity.getContent());
        }
        return R.ok().put("data",arr);
    }

    @Override
    public R addData(String content) {
        BarrageEntity entity = new BarrageEntity();
        entity.setContent(content);
        barrageDao.insert(entity);
        return R.ok();
    }

}